home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch1 / BitEdit.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-06  |  29.4 KB  |  907 lines

  1. VERSION 5.00
  2. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#2.0#0"; "MSCOMCTL.OCX"
  3. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  4. Begin VB.Form Form1 
  5.    Caption         =   "Form1"
  6.    ClientHeight    =   4275
  7.    ClientLeft      =   165
  8.    ClientTop       =   735
  9.    ClientWidth     =   6750
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4275
  12.    ScaleWidth      =   6750
  13.    StartUpPosition =   3  'Windows Default
  14.    Begin VB.PictureBox picDrawStyleSample 
  15.       Height          =   375
  16.       Index           =   0
  17.       Left            =   2520
  18.       ScaleHeight     =   315
  19.       ScaleWidth      =   720
  20.       TabIndex        =   11
  21.       Top             =   2400
  22.       Visible         =   0   'False
  23.       Width           =   780
  24.    End
  25.    Begin VB.PictureBox picDrawStyle 
  26.       Height          =   375
  27.       Left            =   2520
  28.       ScaleHeight     =   315
  29.       ScaleWidth      =   720
  30.       TabIndex        =   10
  31.       Top             =   2880
  32.       Width           =   780
  33.    End
  34.    Begin VB.PictureBox picFillStyleSample 
  35.       Height          =   375
  36.       Index           =   0
  37.       Left            =   3360
  38.       ScaleHeight     =   315
  39.       ScaleWidth      =   720
  40.       TabIndex        =   9
  41.       Top             =   2400
  42.       Visible         =   0   'False
  43.       Width           =   780
  44.    End
  45.    Begin VB.PictureBox picFillStyle 
  46.       Height          =   375
  47.       Left            =   3360
  48.       ScaleHeight     =   315
  49.       ScaleWidth      =   720
  50.       TabIndex        =   8
  51.       Top             =   2880
  52.       Width           =   780
  53.    End
  54.    Begin VB.PictureBox picDrawWidthSample 
  55.       Height          =   375
  56.       Index           =   0
  57.       Left            =   1680
  58.       ScaleHeight     =   315
  59.       ScaleWidth      =   720
  60.       TabIndex        =   7
  61.       Top             =   2400
  62.       Visible         =   0   'False
  63.       Width           =   780
  64.    End
  65.    Begin VB.PictureBox picDrawWidth 
  66.       Height          =   375
  67.       Left            =   1680
  68.       ScaleHeight     =   315
  69.       ScaleWidth      =   720
  70.       TabIndex        =   6
  71.       Top             =   2880
  72.       Width           =   780
  73.    End
  74.    Begin VB.PictureBox picColorSamples 
  75.       Height          =   615
  76.       Left            =   0
  77.       ScaleHeight     =   555
  78.       ScaleWidth      =   555
  79.       TabIndex        =   3
  80.       Top             =   2520
  81.       Width           =   615
  82.       Begin VB.PictureBox picForeColorSample 
  83.          AutoRedraw      =   -1  'True
  84.          Height          =   255
  85.          Left            =   120
  86.          ScaleHeight     =   195
  87.          ScaleWidth      =   195
  88.          TabIndex        =   4
  89.          Top             =   120
  90.          Width           =   255
  91.       End
  92.       Begin VB.PictureBox picFillColorSample 
  93.          AutoRedraw      =   -1  'True
  94.          Height          =   255
  95.          Left            =   240
  96.          ScaleHeight     =   195
  97.          ScaleWidth      =   195
  98.          TabIndex        =   5
  99.          Top             =   240
  100.          Width           =   255
  101.       End
  102.    End
  103.    Begin VB.PictureBox picSwatch 
  104.       Height          =   255
  105.       Index           =   0
  106.       Left            =   840
  107.       ScaleHeight     =   195
  108.       ScaleWidth      =   195
  109.       TabIndex        =   2
  110.       Top             =   2880
  111.       Width           =   255
  112.    End
  113.    Begin VB.PictureBox picCanvas 
  114.       AutoRedraw      =   -1  'True
  115.       BackColor       =   &H00FFFFFF&
  116.       FillColor       =   &H00C0C0C0&
  117.       Height          =   495
  118.       Left            =   0
  119.       ScaleHeight     =   435
  120.       ScaleWidth      =   435
  121.       TabIndex        =   1
  122.       Top             =   840
  123.       Width           =   495
  124.    End
  125.    Begin ComctlLib.Toolbar tbrButtons 
  126.       Align           =   1  'Align Top
  127.       Height          =   630
  128.       Left            =   0
  129.       TabIndex        =   0
  130.       Top             =   0
  131.       Width           =   6750
  132.       _ExtentX        =   11906
  133.       _ExtentY        =   1111
  134.       ButtonWidth     =   609
  135.       ButtonHeight    =   953
  136.       Appearance      =   1
  137.       _Version        =   393216
  138.    End
  139.    Begin ComctlLib.ImageList imlButtons 
  140.       Left            =   960
  141.       Top             =   1320
  142.       _ExtentX        =   1005
  143.       _ExtentY        =   1005
  144.       BackColor       =   -2147483643
  145.       ImageWidth      =   16
  146.       ImageHeight     =   16
  147.       MaskColor       =   12632256
  148.       _Version        =   393216
  149.       BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628} 
  150.          NumListImages   =   8
  151.          BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  152.             Picture         =   "BitEdit.frx":0000
  153.             Key             =   ""
  154.          EndProperty
  155.          BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  156.             Picture         =   "BitEdit.frx":0112
  157.             Key             =   ""
  158.          EndProperty
  159.          BeginProperty ListImage3 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  160.             Picture         =   "BitEdit.frx":0224
  161.             Key             =   ""
  162.          EndProperty
  163.          BeginProperty ListImage4 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  164.             Picture         =   "BitEdit.frx":0336
  165.             Key             =   ""
  166.          EndProperty
  167.          BeginProperty ListImage5 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  168.             Picture         =   "BitEdit.frx":0448
  169.             Key             =   ""
  170.          EndProperty
  171.          BeginProperty ListImage6 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  172.             Picture         =   "BitEdit.frx":055A
  173.             Key             =   ""
  174.          EndProperty
  175.          BeginProperty ListImage7 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  176.             Picture         =   "BitEdit.frx":066C
  177.             Key             =   ""
  178.          EndProperty
  179.          BeginProperty ListImage8 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  180.             Picture         =   "BitEdit.frx":077E
  181.             Key             =   ""
  182.          EndProperty
  183.       EndProperty
  184.    End
  185.    Begin MSComDlg.CommonDialog dlgFile 
  186.       Left            =   2160
  187.       Top             =   1320
  188.       _ExtentX        =   847
  189.       _ExtentY        =   847
  190.       _Version        =   393216
  191.       Filter          =   "Bitmap Files (*.bmp)|*.bmp"
  192.    End
  193.    Begin VB.Menu mnuFile 
  194.       Caption         =   "&File"
  195.       Begin VB.Menu mnuFileNew 
  196.          Caption         =   "&New"
  197.          Shortcut        =   ^N
  198.       End
  199.       Begin VB.Menu mnuFileOpen 
  200.          Caption         =   "&Open"
  201.          Shortcut        =   ^O
  202.       End
  203.       Begin VB.Menu mnuFileSave 
  204.          Caption         =   "&Save"
  205.          Shortcut        =   ^S
  206.       End
  207.       Begin VB.Menu mnuFileSaveAs 
  208.          Caption         =   "Save &As"
  209.       End
  210.       Begin VB.Menu mnuFileSep 
  211.          Caption         =   "-"
  212.       End
  213.       Begin VB.Menu mnuFileExit 
  214.          Caption         =   "E&xit"
  215.       End
  216.    End
  217.    Begin VB.Menu mnuEdit 
  218.       Caption         =   "&Edit"
  219.       Begin VB.Menu mnuEditUndo 
  220.          Caption         =   "&Undo"
  221.          Shortcut        =   ^Z
  222.       End
  223.       Begin VB.Menu mnuEditRedo 
  224.          Caption         =   "&Redo"
  225.          Shortcut        =   ^Y
  226.       End
  227.    End
  228.    Begin VB.Menu mnuDrawWidth 
  229.       Caption         =   "Draw&Width"
  230.       Begin VB.Menu mnuDrawWidthSet 
  231.          Caption         =   "1"
  232.          Index           =   1
  233.       End
  234.       Begin VB.Menu mnuDrawWidthSet 
  235.          Caption         =   "2"
  236.          Index           =   2
  237.       End
  238.       Begin VB.Menu mnuDrawWidthSet 
  239.          Caption         =   "3"
  240.          Index           =   3
  241.       End
  242.       Begin VB.Menu mnuDrawWidthSet 
  243.          Caption         =   "4"
  244.          Index           =   4
  245.       End
  246.       Begin VB.Menu mnuDrawWidthSet 
  247.          Caption         =   "5"
  248.          Index           =   5
  249.       End
  250.    End
  251.    Begin VB.Menu mnuDrawStyle 
  252.       Caption         =   "Draw&Style"
  253.       Begin VB.Menu mnuDrawStyleSet 
  254.          Caption         =   "0"
  255.          Index           =   0
  256.       End
  257.       Begin VB.Menu mnuDrawStyleSet 
  258.          Caption         =   "1"
  259.          Index           =   1
  260.       End
  261.       Begin VB.Menu mnuDrawStyleSet 
  262.          Caption         =   "2"
  263.          Index           =   2
  264.       End
  265.       Begin VB.Menu mnuDrawStyleSet 
  266.          Caption         =   "3"
  267.          Index           =   3
  268.       End
  269.       Begin VB.Menu mnuDrawStyleSet 
  270.          Caption         =   "4"
  271.          Index           =   4
  272.       End
  273.       Begin VB.Menu mnuDrawStyleSet 
  274.          Caption         =   "5"
  275.          Index           =   5
  276.       End
  277.    End
  278.    Begin VB.Menu mnuFillStyle 
  279.       Caption         =   "&FillStyle"
  280.       Begin VB.Menu mnuFillStyleSet 
  281.          Caption         =   "0"
  282.          Index           =   0
  283.       End
  284.       Begin VB.Menu mnuFillStyleSet 
  285.          Caption         =   "1"
  286.          Index           =   1
  287.       End
  288.       Begin VB.Menu mnuFillStyleSet 
  289.          Caption         =   "2"
  290.          Index           =   2
  291.       End
  292.       Begin VB.Menu mnuFillStyleSet 
  293.          Caption         =   "3"
  294.          Index           =   3
  295.       End
  296.       Begin VB.Menu mnuFillStyleSet 
  297.          Caption         =   "4"
  298.          Index           =   4
  299.       End
  300.       Begin VB.Menu mnuFillStyleSet 
  301.          Caption         =   "5"
  302.          Index           =   5
  303.       End
  304.       Begin VB.Menu mnuFillStyleSet 
  305.          Caption         =   "6"
  306.          Index           =   6
  307.       End
  308.       Begin VB.Menu mnuFillStyleSet 
  309.          Caption         =   "7"
  310.          Index           =   7
  311.       End
  312.    End
  313. Attribute VB_Name = "Form1"
  314. Attribute VB_GlobalNameSpace = False
  315. Attribute VB_Creatable = False
  316. Attribute VB_PredeclaredId = True
  317. Attribute VB_Exposed = False
  318. Option Explicit
  319. ' Tool variables.
  320. Private Enum ToolTypes
  321.     tool_Point = 1
  322.     tool_Line
  323.     tool_Rectangle
  324.     tool_Ellipse
  325.     tool_Scribble
  326.     tool_Polyline
  327.     tool_Undo
  328.     tool_Redo
  329. End Enum
  330. Private SelectedTool As Integer
  331. ' Undo/redo variables.
  332. Private Const NUM_UNDOS = 10
  333. Private LastCheckpoint As Integer
  334. Private Checkpoints As Collection
  335. ' File variables.
  336. Private FileName As String
  337. Private FileTitle As String
  338. ' Drawing variables.
  339. Private Drawing As Boolean
  340. Private FirstX As Single
  341. Private FirstY As Single
  342. Private LastX As Single
  343. Private LastY As Single
  344. Private DataModified As Boolean
  345. ' API stuff for putting bitmaps in menus.
  346. Private Type MENUITEMINFO
  347.     cbSize As Long
  348.     fMask As Long
  349.     fType As Long
  350.     fState As Long
  351.     wid As Long
  352.     hSubMenu As Long
  353.     hbmpChecked As Long
  354.     hbmpUnchecked As Long
  355.     dwItemData As Long
  356.     dwTypeData As Long
  357.     cch As Long
  358. End Type
  359. Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
  360. Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
  361. Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal bypos As Long, lpcMenuItemInfo As MENUITEMINFO) As Long
  362. Private Const MF_BITMAP = &H4&
  363. Private Const MFT_BITMAP = MF_BITMAP
  364. Private Const MIIM_TYPE = &H10
  365. ' See if it is safe to discard the data.
  366. Private Function DataSafe() As Boolean
  367.     If Not DataModified Then
  368.         ' The data has not been modified. It's safe.
  369.         DataSafe = True
  370.     Else
  371.         ' Ask the user if we should save changes.
  372.         Select Case MsgBox("The data has been modified. Do you want to save the changes?", vbYesNoCancel)
  373.             Case vbYes
  374.                 ' Save the data.
  375.                 'mnuFileSave_Click
  376.                 DataSafe = Not DataModified
  377.             Case vbNo
  378.                 DataSafe = True
  379.             Case vbCancel
  380.                 DataSafe = False
  381.         End Select
  382.     End If
  383. End Function
  384. ' Draw a color sample.
  385. Private Sub DrawSample()
  386.     picFillColorSample.Line (0, 0)-(1000, 1000), picCanvas.FillColor, BF
  387.     picForeColorSample.Line (0, 0)-(1000, 1000), picCanvas.ForeColor, BF
  388. End Sub
  389. ' Draw the shape for the selected tool.
  390. Private Sub DrawShape()
  391. Dim cx As Single
  392. Dim cy As Single
  393. Dim wid As Single
  394. Dim hgt As Single
  395.     Select Case SelectedTool
  396.         Case tool_Point
  397.             picCanvas.PSet (LastX, LastY)
  398.         Case tool_Line
  399.             picCanvas.Line (FirstX, FirstY)-(LastX, LastY)
  400.         Case tool_Rectangle
  401.             picCanvas.Line (FirstX, FirstY)-(LastX, LastY), , B
  402.         Case tool_Ellipse
  403.             wid = Abs(LastX - FirstX)
  404.             hgt = Abs(LastY - FirstY)
  405.             If wid = 0 Or hgt = 0 Then Exit Sub
  406.             cx = (FirstX + LastX) / 2
  407.             cy = (FirstY + LastY) / 2
  408.             If wid > hgt Then
  409.                 picCanvas.Circle (cx, cy), wid / 2, , , , hgt / wid
  410.             Else
  411.                 picCanvas.Circle (cx, cy), hgt / 2, , , , hgt / wid
  412.             End If
  413.         Case tool_Scribble
  414.             picCanvas.Line -(LastX, LastY)
  415.         Case tool_Polyline
  416.             picCanvas.Line (FirstX, FirstY)-(LastX, LastY)
  417.     End Select
  418. End Sub
  419. ' Set DataModified = True to indicate the data has
  420. ' been changed. Save the changes for undo/redo.
  421. Private Sub SetModified()
  422.     ' Update the caption if necessary.
  423.     If Not DataModified Then Caption = "BitEdit*[" & FileTitle & "]"
  424.     DataModified = True
  425. End Sub
  426. ' Save the picture for undo/redo.
  427. Private Sub SaveCheckpoint()
  428. Dim new_picture As StdPicture
  429. Dim i As Integer
  430.     ' Get the next checkpoint index.
  431.     LastCheckpoint = LastCheckpoint + 1
  432.     ' Remove any checkpoints after the current one.
  433.     Do While Checkpoints.Count >= LastCheckpoint
  434.         Checkpoints.Remove Checkpoints.Count
  435.     Loop
  436.     ' See if we have too many stored.
  437.     If LastCheckpoint > NUM_UNDOS Then
  438.         ' Too many. Drop the oldest image.
  439.         Checkpoints.Remove 1
  440.         LastCheckpoint = LastCheckpoint - 1
  441.     End If
  442.     ' Save the current image.
  443.     picCanvas.Picture = picCanvas.Image
  444.     Set new_picture = New StdPicture
  445.     Set new_picture = picCanvas.Picture
  446.     Checkpoints.Add new_picture
  447.     ' Enable and disable the undo buttons.
  448.     SetUndoButtons
  449. End Sub
  450. ' Enable the appropriate undo buttons.
  451. Private Sub SetUndoButtons()
  452. Dim enable_undo As Boolean
  453. Dim enable_redo As Boolean
  454.     enable_undo = (LastCheckpoint > 1)
  455.     enable_redo = (LastCheckpoint < Checkpoints.Count)
  456.     If enable_undo <> mnuEditUndo.Enabled Then
  457.         tbrButtons.Buttons("Undo").Enabled = enable_undo
  458.         mnuEditUndo.Enabled = enable_undo
  459.     End If
  460.     If enable_redo <> mnuEditRedo.Enabled Then
  461.         tbrButtons.Buttons("Redo").Enabled = enable_redo
  462.         mnuEditRedo.Enabled = enable_redo
  463.     End If
  464. End Sub
  465. Private Sub Form_Load()
  466. Dim btn As Button
  467. Dim i As Integer
  468. Dim tips(tool_Point To tool_Redo) As String
  469. Dim pos As Single
  470. Dim main_menu As Long
  471. Dim sub_menu As Long
  472. Dim menu_info As MENUITEMINFO
  473.     dlgFile.InitDir = App.Path
  474.     ' Load the tool tips.
  475.     tips(tool_Point) = "Point"
  476.     tips(tool_Line) = "Line"
  477.     tips(tool_Rectangle) = "Rectangle"
  478.     tips(tool_Ellipse) = "Ellipse"
  479.     tips(tool_Scribble) = "Scribble"
  480.     tips(tool_Polyline) = "Polyline"
  481.     tips(tool_Undo) = "Undo"
  482.     tips(tool_Redo) = "Redo"
  483.     ' Load the tool buttons.
  484.     tbrButtons.ImageList = imlButtons
  485.     For i = tool_Point To tool_Redo
  486.         Set btn = tbrButtons.Buttons.Add(, , , , i)
  487.         btn.ToolTipText = tips(i)
  488.         btn.Key = tips(i)
  489.     Next i
  490.     ' Create color swatches.
  491.     For i = 0 To 15
  492.         If i > 0 Then
  493.             Load picSwatch(i)
  494.             picSwatch(i).Visible = True
  495.         End If
  496.         picSwatch(i).BackColor = QBColor(i)
  497.     Next i
  498.     picColorSamples.Height = 2 * picSwatch(0).Height + 30
  499.     picColorSamples.Width = picColorSamples.Height
  500.     pos = picColorSamples.ScaleWidth * 0.1
  501.     picForeColorSample.Move pos, pos
  502.     pos = picColorSamples.ScaleWidth * 0.9 - picFillColorSample.Width
  503.     picFillColorSample.Move pos, pos
  504.     ' Create the DrawWidth menu.
  505.     main_menu = GetMenu(hwnd)
  506.     sub_menu = GetSubMenu(main_menu, 2)
  507.     For i = 1 To 5
  508.         Load picDrawWidthSample(i)
  509.         picDrawWidthSample(i).AutoRedraw = True
  510.         picDrawWidthSample(i).DrawWidth = i
  511.         picDrawWidthSample(i).Line (-1000, picDrawWidthSample(0).ScaleHeight / 2)-Step(2000, 0)
  512.         picDrawWidthSample(i).Picture = picDrawWidthSample(i).Image
  513.         With menu_info
  514.             .cbSize = Len(menu_info)
  515.             .fMask = MIIM_TYPE
  516.             .fType = MFT_BITMAP
  517.             .dwTypeData = picDrawWidthSample(i).Picture
  518.         End With
  519.         SetMenuItemInfo sub_menu, i - 1, True, menu_info
  520.     Next i
  521.     ' Start with DrawWidth = 1.
  522.     mnuDrawWidthSet_Click 1
  523.     ' Create the DrawStyle menu.
  524.     main_menu = GetMenu(hwnd)
  525.     sub_menu = GetSubMenu(main_menu, 3)
  526.     For i = 0 To 5
  527.         If i > 0 Then Load picDrawStyleSample(i)
  528.         picDrawStyleSample(i).AutoRedraw = True
  529.         picDrawStyleSample(i).Line (0, 0)-(2000, 2000), picDrawStyleSample(0).BackColor, BF
  530.         picDrawStyleSample(i).DrawStyle = i
  531.         picDrawStyleSample(i).Line (-1000, picDrawStyleSample(0).ScaleHeight / 2)-Step(2000, 0)
  532.         picDrawStyleSample(i).Picture = picDrawStyleSample(i).Image
  533.         With menu_info
  534.             .cbSize = Len(menu_info)
  535.             .fMask = MIIM_TYPE
  536.             .fType = MFT_BITMAP
  537.             .dwTypeData = picDrawStyleSample(i).Picture
  538.         End With
  539.         SetMenuItemInfo sub_menu, i, True, menu_info
  540.     Next i
  541.     ' Start with Drawstyle = vbSolid.
  542.     mnuDrawStyleSet_Click vbSolid
  543.     ' Create the fillstyle menu.
  544.     main_menu = GetMenu(hwnd)
  545.     sub_menu = GetSubMenu(main_menu, 4)
  546.     For i = 0 To 7
  547.         If i > 0 Then Load picFillStyleSample(i)
  548.         picFillStyleSample(i).AutoRedraw = True
  549.         picFillStyleSample(i).FillStyle = vbFSSolid
  550.         picFillStyleSample(i).Line (-1000, -1000)-(2000, 2000), picFillStyleSample(0).BackColor, BF
  551.         picFillStyleSample(i).FillStyle = i
  552.         picFillStyleSample(i).Line (-1000, -1000)-(2000, 2000), , B
  553.         picFillStyleSample(i).Picture = picFillStyleSample(i).Image
  554.         With menu_info
  555.             .cbSize = Len(menu_info)
  556.             .fMask = MIIM_TYPE
  557.             .fType = MFT_BITMAP
  558.             .dwTypeData = picFillStyleSample(i).Picture
  559.         End With
  560.         SetMenuItemInfo sub_menu, i, True, menu_info
  561.     Next i
  562.     ' Start with fillstyle = vbFSTransparent.
  563.     mnuFillStyleSet_Click vbFSTransparent
  564.     ' Start a new project.
  565.     mnuFileNew_Click
  566.     ' Draw the initial sample.
  567.     DrawSample
  568.     ' Select the point tool.
  569.     tbrButtons_ButtonClick tbrButtons.Buttons(tool_Point)
  570. End Sub
  571. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  572.     Cancel = Not DataSafe
  573. End Sub
  574. Private Sub Form_Resize()
  575. Dim hgt As Single
  576. Dim t As Single
  577. Dim i As Integer
  578.     If WindowState = vbMinimized Then Exit Sub
  579.     t = ScaleHeight - picColorSamples.Height
  580.     picColorSamples.Top = t
  581.     picSwatch(0).Move picColorSamples.Width + 30, picColorSamples.Top
  582.     For i = 0 To 15
  583.         picSwatch(i).Visible = False
  584.     Next i
  585.     For i = 1 To 15
  586.         If i = 8 Then
  587.             picSwatch(i).Left = picSwatch(0).Left
  588.             picSwatch(i).Top = picSwatch(0).Top + picSwatch(0).Height + 30
  589.         Else
  590.             picSwatch(i).Left = picSwatch(i - 1).Left + picSwatch(i - 1).Width + 30
  591.             picSwatch(i).Top = picSwatch(i - 1).Top
  592.         End If
  593.     Next i
  594.     For i = 0 To 15
  595.         picSwatch(i).Visible = True
  596.     Next i
  597.     hgt = picColorSamples.Top - tbrButtons.Height - 30
  598.     If hgt <= 0 Then Exit Sub
  599.     picCanvas.Move 0, tbrButtons.Height, ScaleWidth, hgt
  600.     picDrawWidth.Move picSwatch(7).Left + _
  601.         picSwatch(7).Width + 120, _
  602.         picSwatch(7).Top
  603.     picDrawStyle.Move picDrawWidth.Left + _
  604.         picDrawWidth.Width + 120, _
  605.         picDrawWidth.Top
  606.     picFillStyle.Move picDrawStyle.Left + _
  607.         picDrawStyle.Width + 120, _
  608.         picDrawStyle.Top
  609. End Sub
  610. ' Set the DrawStyle.
  611. Private Sub mnuDrawStyleSet_Click(Index As Integer)
  612. Dim i As Integer
  613.     ' Check the selected style.
  614.     For i = 0 To 5
  615.         mnuDrawStyleSet(i).Checked = False
  616.     Next i
  617.     mnuDrawStyleSet(Index).Checked = True
  618.     ' Display the selected style.
  619.     picDrawStyle.Picture = picDrawStyleSample(Index).Picture
  620.     ' Select the DrawStyle.
  621.     picCanvas.DrawStyle = Index
  622. End Sub
  623. ' Redo the previously undone command.
  624. Private Sub mnuEditRedo_Click()
  625.     LastCheckpoint = LastCheckpoint + 1
  626.     picCanvas.Picture = Checkpoints(LastCheckpoint)
  627.     SetUndoButtons
  628.     ' Flag the data as modified.
  629.     SetModified
  630. End Sub
  631. ' Undo the previous command.
  632. Private Sub mnuEditUndo_Click()
  633.     LastCheckpoint = LastCheckpoint - 1
  634.     picCanvas.Picture = Checkpoints(LastCheckpoint)
  635.     ' Enable and disable the undo buttons.
  636.     SetUndoButtons
  637.     ' Flag the data as modified.
  638.     SetModified
  639. End Sub
  640. ' Unload the form. The QueryUnload event handler
  641. ' will make sure it's safe to do so.
  642. Private Sub mnuFileExit_Click()
  643.     Unload Me
  644. End Sub
  645. ' Start a new project.
  646. Private Sub mnuFileNew_Click()
  647.     ' Make sure the data is safe.
  648.     If Not DataSafe() Then Exit Sub
  649.     ' Start a new project.
  650.     picCanvas.Line (0, 0)-(picCanvas.ScaleWidth, picCanvas.ScaleHeight), picCanvas.BackColor, BF
  651.     ' Start a new Checkpoints collection.
  652.     Set Checkpoints = New Collection
  653.     LastCheckpoint = 0
  654.     ' Checkpoint the blank project.
  655.     SaveCheckpoint
  656.     DataModified = False
  657.     Caption = "BitEdit []"
  658.     FileName = ""
  659.     FileTitle = ""
  660. End Sub
  661. ' Open a file.
  662. Private Sub mnuFileOpen_Click()
  663.     ' Make sure the data is safe.
  664.     If Not DataSafe() Then Exit Sub
  665.     ' Let the user select a file name.
  666.     dlgFile.Flags = _
  667.         cdlOFNExplorer + _
  668.         cdlOFNHideReadOnly + _
  669.         cdlOFNLongNames + _
  670.         cdlOFNFileMustExist
  671.     dlgFile.CancelError = True
  672.     On Error Resume Next
  673.     dlgFile.ShowOpen
  674.     If Err.Number = cdlCancel Then
  675.         Exit Sub
  676.     ElseIf Err.Number > 0 Then
  677.         MsgBox "Error " & Format$(Err.Number) & _
  678.             " selecting the file." & _
  679.             vbCrLf & Err.Description
  680.         Exit Sub
  681.     End If
  682.     On Error GoTo 0
  683.     ' Open the file.
  684.     On Error GoTo OpenErr
  685.     picCanvas.Picture = LoadPicture(dlgFile.FileName)
  686.     ' Start a new Checkpoints collection.
  687.     Set Checkpoints = New Collection
  688.     LastCheckpoint = 0
  689.     ' Checkpoint the new file.
  690.     SaveCheckpoint
  691.     ' Update the file name and title.
  692.     FileName = dlgFile.FileName
  693.     FileTitle = dlgFile.FileTitle
  694.     Caption = "BitEdit [" & FileTitle & "]"
  695.     DataModified = False
  696.     Exit Sub
  697. OpenErr:
  698.     MsgBox "Error " & Format$(Err.Number) & _
  699.         " saving file '" & dlgFile.FileName & "'." & _
  700.         vbCrLf & Err.Description
  701.     Exit Sub
  702.     ' Update the file name and title.
  703.     FileName = dlgFile.FileName
  704.     FileTitle = dlgFile.FileTitle
  705.     Caption = "BitEdit [" & FileTitle & "]"
  706.     DataModified = False
  707. End Sub
  708. ' Save the file.
  709. Private Sub mnuFileSave_Click()
  710.     ' If there is no file name, treat as Save As.
  711.     If Len(FileName) = 0 Then
  712.         mnuFileSaveAs_Click
  713.         Exit Sub
  714.     End If
  715.     ' Save the file.
  716.     On Error GoTo SaveErr
  717.     SavePicture picCanvas.Picture, FileName
  718.     ' Update the file name and title.
  719.     FileName = dlgFile.FileName
  720.     FileTitle = dlgFile.FileTitle
  721.     Caption = "BitEdit [" & FileTitle & "]"
  722.     DataModified = False
  723.     Exit Sub
  724. SaveErr:
  725.     MsgBox "Error " & Format$(Err.Number) & _
  726.         " saving file '" & FileName & "'." & _
  727.         vbCrLf & Err.Description
  728.     Exit Sub
  729. End Sub
  730. ' Save the file with a new name.
  731. Private Sub mnuFileSaveAs_Click()
  732.     ' Let the user select a file name.
  733.     dlgFile.Flags = _
  734.         cdlOFNExplorer + _
  735.         cdlOFNHideReadOnly + _
  736.         cdlOFNLongNames + _
  737.         cdlOFNOverwritePrompt + _
  738.         cdlOFNPathMustExist
  739.     dlgFile.CancelError = True
  740.     On Error Resume Next
  741.     dlgFile.ShowSave
  742.     If Err.Number = cdlCancel Then
  743.         Exit Sub
  744.     ElseIf Err.Number > 0 Then
  745.         MsgBox "Error " & Format$(Err.Number) & _
  746.             " selecting the file." & _
  747.             vbCrLf & Err.Description
  748.         Exit Sub
  749.     End If
  750.     On Error GoTo 0
  751.     ' Save the file.
  752.     On Error GoTo SaveAsErr
  753.     SavePicture picCanvas.Picture, dlgFile.FileName
  754.     ' Update the file name and title.
  755.     FileName = dlgFile.FileName
  756.     FileTitle = dlgFile.FileTitle
  757.     Caption = "BitEdit [" & FileTitle & "]"
  758.     DataModified = False
  759.     Exit Sub
  760. SaveAsErr:
  761.     MsgBox "Error " & Format$(Err.Number) & _
  762.         " saving file '" & dlgFile.FileName & "'." & _
  763.         vbCrLf & Err.Description
  764.     Exit Sub
  765. End Sub
  766. ' Set the DrawWidth.
  767. Private Sub mnuDrawWidthSet_Click(Index As Integer)
  768. Dim i As Integer
  769.     ' Check the selected width.
  770.     For i = 1 To 5
  771.         mnuDrawWidthSet(i).Checked = False
  772.     Next i
  773.     mnuDrawWidthSet(Index).Checked = True
  774.     ' Display the selected width.
  775.     picDrawWidth.Picture = picDrawWidthSample(Index).Picture
  776.     ' Select the DrawWidth.
  777.     picCanvas.DrawWidth = Index
  778. End Sub
  779. ' Set the FillStyle.
  780. Private Sub mnuFillStyleSet_Click(Index As Integer)
  781. Dim i As Integer
  782.     ' Check the selected style.
  783.     For i = 0 To 7
  784.         mnuFillStyleSet(i).Checked = False
  785.     Next i
  786.     mnuFillStyleSet(Index).Checked = True
  787.     ' Display the selected style.
  788.     picFillStyle.Picture = picFillStyleSample(Index).Picture
  789.     ' Select the fillstyle.
  790.     picCanvas.FillStyle = Index
  791. End Sub
  792. ' Start doing something.
  793. Private Sub picCanvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  794.     ' See if we are ending a polyline.
  795.     If (SelectedTool = tool_Polyline) And _
  796.        (Button = vbRightButton) And Drawing _
  797.     Then
  798.         ' End the polyline.
  799.         Drawing = False
  800.         ' Erase the last segment.
  801.         DrawShape
  802.         ' Mark the data and save a checkpoint.
  803.         SetModified
  804.         SaveCheckpoint
  805.         Exit Sub
  806.     End If
  807.     ' See if we are drawing a polyline.
  808.     If (SelectedTool = tool_Polyline) And Drawing Then
  809.         ' Finalize the segment.
  810.         picCanvas.DrawMode = vbCopyPen
  811.         DrawShape
  812.     End If
  813.     ' Deal with other situations.
  814.     ' Save the coordinates.
  815.     FirstX = X
  816.     FirstY = Y
  817.     LastX = X
  818.     LastY = Y
  819.     ' Prepare to draw in invert mode.
  820.     If SelectedTool = tool_Scribble Then
  821.         picCanvas.CurrentX = X
  822.         picCanvas.CurrentY = Y
  823.     ElseIf SelectedTool = tool_Polyline Then
  824.         ' See if we are not already drawing.
  825.         If Not Drawing Then
  826.             ' Start the first segment here.
  827.             picCanvas.CurrentX = X
  828.             picCanvas.CurrentY = Y
  829.         End If
  830.         picCanvas.DrawMode = vbInvert
  831.     Else
  832.         picCanvas.DrawMode = vbInvert
  833.     End If
  834.     Drawing = True
  835.     ' Draw the initial shape.
  836.     DrawShape
  837. End Sub
  838. Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  839.     If Not Drawing Then Exit Sub
  840.     ' Erase the previous shape.
  841.     DrawShape
  842.     LastX = X
  843.     LastY = Y
  844.     ' Draw the new shape.
  845.     DrawShape
  846. End Sub
  847. Private Sub picCanvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  848.     If Not Drawing Then Exit Sub
  849.     ' Do nothing if we are drawing a polyline.
  850.     ' All the interesting stuff happens in the
  851.     ' MouseDown and MouseMove event handlers.
  852.     If SelectedTool <> tool_Polyline Then
  853.         Drawing = False
  854.         ' Erase the previous shape.
  855.         DrawShape
  856.         LastX = X
  857.         LastY = Y
  858.         ' Draw the final shape.
  859.         picCanvas.DrawMode = vbCopyPen
  860.         DrawShape
  861.         ' Mark the data and save a checkpoint.
  862.         SetModified
  863.         SaveCheckpoint
  864.     End If
  865. End Sub
  866. ' Display the DrawStyle popup.
  867. Private Sub picDrawStyle_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  868.     PopupMenu mnuDrawStyle
  869. End Sub
  870. ' Display the DrawWidth popup.
  871. Private Sub picDrawWidth_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  872.     PopupMenu mnuDrawWidth
  873. End Sub
  874. ' Display the FillStyle popup.
  875. Private Sub picFillStyle_Click()
  876.     PopupMenu mnuFillStyle
  877. End Sub
  878. ' Select the new color.
  879. Private Sub picSwatch_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
  880.     If Button = vbLeftButton Then
  881.         picCanvas.ForeColor = QBColor(Index)
  882.     Else
  883.         picCanvas.FillColor = QBColor(Index)
  884.     End If
  885.     ' Draw a new color sample.
  886.     DrawSample
  887. End Sub
  888. ' Process a toolbar button click.
  889. Private Sub tbrButtons_ButtonClick(ByVal Button As ComctlLib.Button)
  890.     ' See what kind of button this is.
  891.     If Button.Index <= tool_Polyline Then
  892.         ' This is a toggle button.
  893.         ' Deselect the previously selected tool.
  894.         If SelectedTool > 0 Then tbrButtons.Buttons(SelectedTool).Value = tbrUnpressed
  895.         ' Select the new tool.
  896.         SelectedTool = Button.Index
  897.         tbrButtons.Buttons(SelectedTool).Value = tbrPressed
  898.         tbrButtons.Refresh
  899.     ElseIf Button.Index = tool_Undo Then
  900.         ' Undo the previous command.
  901.         mnuEditUndo_Click
  902.     ElseIf Button.Index = tool_Redo Then
  903.         ' Redo the previously undone command.
  904.         mnuEditRedo_Click
  905.     End If
  906. End Sub
  907.